//
//  HMHomeTableViewCell.swift
//  WeiBo
//
//  Created by Apple on 2017/5/31.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit

/*
    1. 原创微博视图
    2. 转发微博视图
    3. toolBar视图
 */

//  自定义首页cell
class HMHomeTableViewCell: UITableViewCell {
    
    //  记录上一次toolbar顶部约束
    var toolBarTopConstraint: Constraint?
    
    //  引用ViewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
            originalView.statusViewModel = statusViewModel
            //  先把之前的约束卸载掉
            toolBarTopConstraint?.deactivate()
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博
                //  显示转发微博
                retweetView.isHidden = false
                //  绑定数据
                retweetView.statusViewModel = statusViewModel
                //  创建约束
                toolBar.snp.makeConstraints({ (make) in
                     toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
            } else {
                //  没有转发微博
                //  隐藏转发微博
                retweetView.isHidden = true
                //  创建约束
                toolBar.snp.makeConstraints({ (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }
            
        }
    }
    
    //  MARK:   --懒加载控件
    fileprivate lazy var originalView: HMHomeOriginalView = HMHomeOriginalView()
    fileprivate lazy var retweetView: HMHomeRetweetView = HMHomeRetweetView()
    fileprivate lazy var toolBar: HMHomeToolBar = HMHomeToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  MARK:   --添加控件设置约束
    private func setupUI() {
        
        //  设置contentView的背景色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        //  设置约束
        originalView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.trailing.equalTo(contentView)
        }
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
        }
        toolBar.snp.makeConstraints { (make) in
            toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.leading.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            //  添加关键约束, 让toolbar的底部等于contentView的底部
            make.bottom.equalTo(contentView)
        }
        
        
        
        
        
    }
   
}
